/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.Dao;

import Controler.CXuLy;
import Model.Pojo.Nhanvien;
import Model.Pojo.Parameters;
import Model.Util.HibernateUtil;
import java.text.ParseException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author THANH QUAN
 */
public class DAONhanVien
{

    public List<Nhanvien> DanhSachNhanVien(String hql) throws Exception
    {
        List<Nhanvien> dsNhanVien = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            dsNhanVien = DAOProvider.Select(session, hql, new Parameters[]
            {
            });
        }
        catch (HibernateException ex)
        {
            System.out.println(ex.getMessages());
        }
        finally
        {
            session.close();
        }
        return dsNhanVien;
    }

    public void ThemNhanVien(Nhanvien nv) throws Exception
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            DAOProvider.Insert(session, nv);
        }
        catch (HibernateException ex)
        {
            System.out.println(ex.getMessages());
        }
        finally
        {
            session.close();
        }
    }

    public Nhanvien GetNhanVien(int id) throws Exception
    {
        Nhanvien nv = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            nv = (Nhanvien) DAOProvider.SelectOne(session, Nhanvien.class, id);
        }
        catch (HibernateException ex)
        {
            System.out.println(ex.getMessages());
        }
        finally
        {
            session.close();
        }
        return nv;
    }

    public void DeleteNhanVien(int id)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            Query query = session.createSQLQuery(
                    "update Nhanvien set Tinhtrang = 'False' where id=:id").setParameter("id", id);
            query.executeUpdate();
        }
        catch (HibernateException ex)
        {
            System.out.println(ex.getMessages());
        }
        finally
        {
            session.close();
        }
    }

    public void CapNhatNhanVien(Nhanvien nv) throws Exception
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            DAOProvider.Update(session, nv);
        }
        catch (HibernateException ex)
        {
            System.out.println(ex.getMessages());
        }
        finally
        {
            session.close();
        }
    }

    /**
     * Kiem tra dang nhap
     *
     * @param user
     * @param pass
     * @return
     */
    public Nhanvien DangNhap(String user, String pass)
    {
        Nhanvien nv = null;

        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        try
        {
            String hql = "from Nhanvien nv where nv.manv =:user and nv.matkhau =:pass";
            List list = DAOProvider.Select(session, hql, new Parameters[]
            {
                new Parameters("user", user), new Parameters("pass", pass)
            });
            if (list != null)
            {
                if (!list.isEmpty())
                {
                    nv = (Nhanvien) list.get(0);
                }
            }
        }
        catch (HibernateException ex)
        {
            System.out.println(ex.getMessages());
        }
        catch (Exception ex)
        {
            Logger.getLogger(DAONhanVien.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            session.close();
        }
        return nv;
    }

    /**
     * Tim kiem nhan vien theo cac tieu chi sau
     *
     * @param loainhanvien
     * @param manhanvien
     * @param tennhanvien
     * @param diachi
     * @param dienthoai
     * @param email
     * @return
     */
    public List<Nhanvien> TimKiemNhanVien(int loainhanvien, String manhanvien, String tennhanvien,
                                          String diachi, String dienthoai, String email)
    {
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        List list = null;
        String hql = "";

        if (!"".equals(manhanvien))
        {
            hql = "from Nhanvien nv inner join fetch nv.loainhanvien lnv ";
            hql += "where nv.tinhtrang = true and lnv.id=:loainhanvien and (nv.manv=:manhanvien)";
            try
            {
                list = DAOProvider.Select(session, hql, new Parameters[]
                {
                    new Parameters("loainhanvien", loainhanvien), new Parameters("manhanvien",
                                                                                 manhanvien)
                });
            }
            catch (Exception ex)
            {
                Logger.getLogger(DAOSanPham.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            finally
            {
                session.close();
            }
        }
        else
        {
            hql = "from Nhanvien nv inner join fetch nv.loainhanvien lnv ";
            hql += "where nv.tinhtrang = true and lnv.id=:loainhanvien and nv.hoten like :tennhanvien and nv.diachi like :diachi and nv.diachi like :dienthoai and nv.email like :email";
            try
            {
                list = DAOProvider.Select(session, hql, new Parameters[]
                {
                    new Parameters("loainhanvien", loainhanvien), new Parameters("tennhanvien",
                                                                                 "%" + tennhanvien + "%"),
                    new Parameters("diachi",
                                   "%" + diachi + "%"), new Parameters("dienthoai",
                                                                       "%" + dienthoai + "%"),
                    new Parameters("email",
                                   "%" + email + "%")
                });


            }
            catch (Exception ex)
            {
                Logger.getLogger(DAOSanPham.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
            finally
            {
                session.close();
            }
        }
        return list;
    }
}
